package com.liugui;

import java.util.concurrent.*;

/**
 * @ClassName: CallableDemo
 * @Author: liugui
 * @Date: 2021-05-22 21:46
 **/
public class CallableDemo {
    public static void main(String[] args) {
        //taskFuture();
        futureTask();
    }

    /**
     * FutureTask 实现类 初尝试
     */
    public static void futureTask(){
        ExecutorService executorService = Executors.newCachedThreadPool();
        FutureTask<Integer> task = new FutureTask<>(new Task());
        executorService.submit(task);
        try {
            System.out.println("返回值：" + task.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 返回值初尝试
     */
    public static void taskFuture(){
        //线程池
        ExecutorService executorService = Executors.newCachedThreadPool();
        //支持返回值
        Task task = new Task();
        Future<Integer> future = executorService.submit(task);
        try {
            System.out.println("返回值：" +future.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Task implements Callable<Integer>{

    @Override
    public Integer call() throws Exception {
        Thread.sleep(1000);
        return 2;
    }


}